package org.example.music_manage_system.controller;

import org.example.music_manage_system.pojo.Music;
import org.example.music_manage_system.pojo.PageBean;
import org.example.music_manage_system.pojo.Result;
import org.example.music_manage_system.service.MusicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/music")
public class MusicController {
    @Autowired
    private MusicService musicService;

    //新增音乐
    @PostMapping
    public Result add(@RequestBody @Validated(Music.add.class) Music music) {
        musicService.add(music);
        return Result.success();
    }

    //查找音乐
    @GetMapping("/detail")
    public Result detail(Integer id) {
        Music music = musicService.findById(id);
        return Result.success(music);
    }

    //更改音乐
    @PutMapping
    public Result update(@RequestBody @Validated(Music.update.class) Music music) {
        musicService.update(music);
        return Result.success();
    }

    //删除音乐
    @DeleteMapping
    public Result delete(Integer id) {
        musicService.delete(id);
        return Result.success();
    }

    //分页显示
    @GetMapping
    public Result<PageBean<Music>> list(
            Integer pageNum,
            Integer pageSize,
            @RequestParam(required = false) Integer categoryId,
            @RequestParam(required = false) String states
    ) {
        PageBean<Music> pb =  musicService.list(pageNum,pageSize,categoryId,states);
        return Result.success(pb);
    }


}
